﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using Cashier_DAO.db;

namespace Cashier_Service
{
    /// <summary>
    /// 数据库升级
    /// </summary>
    public class UpgradeService
    {
        /// <summary>
        /// 批量执行SQL
        /// </summary>
        /// <param name="sqlList"></param>
        public void BatchExecuteSQL(List<String> sqlList)
        {
            if (sqlList != null && sqlList.Count > 0)
            {
                using (SQLiteConnection conn = SQLiteConnectionInstance.GetSQLiteConnection())
                {
                    SQLiteTransaction trans = null;
                    try
                    {
                        conn.Open();
                        trans = conn.BeginTransaction();
                        SQLiteCommand cmd = new SQLiteCommand(conn);
                        foreach (String sql in sqlList)
                        {
                            cmd.CommandText = sql;
                            cmd.ExecuteNonQuery();
                        }
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        if (trans != null)
                        {
                            trans.Rollback();
                        }
                        throw ex;
                    }
                    finally
                    {
                        if (trans != null)
                        {
                            trans = null;
                        }
                    }
                }
            }
        }
    }
}
